package com.atguigu.test.mvc.config;

import com.atguigu.test.mvc.interceptor.MyInterceptor;
import com.atguigu.test.mvc.my.MyRequestMappingHandlerAdapter;
import com.atguigu.test.mvc.my.MyTokenHandlerMethodArgumentResolver;
import com.atguigu.test.po.LifeCycleBean;
import com.atguigu.test.processor.MyLifeCycleBeanPostProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zp
 * @version 1.0
 * @description: springWeb 配置类
 * @date 2022/12/14 19:39
 */
@Slf4j
@Configuration
@EnableAspectJAutoProxy
public class SpringWebConfig{


    /**
     * @return RequestMappingHandlerMapping
     * @description: 默认注入的RequestMappingHandlerMapping的是属于DispatcherServlet的一个属性
     * 无法从容器中直接获取 所以需要通过bean手动注入的方式注入对象 方便直接从容器中获取元素
     * @author zp
     * @date: 2022/12/31 16:50
     */
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping();
    }

    /**
     * @return RequestMappingHandlerAdapter
     * @description: 默认注入的RequestMappingHandlerAdapter的是属于DispatcherServlet的一个属性
     * 无法从容器中直接获取 所以需要通过bean手动注入的方式注入对象 方便直接从容器中获取元素
     * @author zp
     * @date: 2023/1/1 15:52
     */
    @Bean
    public MyRequestMappingHandlerAdapter requestMappingHandlerAdapter() {
        MyTokenHandlerMethodArgumentResolver myTokenHandlerMethodArgumentResolver = new MyTokenHandlerMethodArgumentResolver();


        MyRequestMappingHandlerAdapter myRequestMappingHandlerAdapter = new MyRequestMappingHandlerAdapter();
        List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
//        List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
//        returnValueHandlers.add(myYamlHandlerMethodReturnValueHandler);
//        returnValueHandlers.add(myPrivacyEncryptHandlerMethodReturnValueHandler);
        argumentResolvers.add(myTokenHandlerMethodArgumentResolver);
        // 添加自定义的参数解析器
        myRequestMappingHandlerAdapter.setCustomArgumentResolvers(argumentResolvers);
        // 添加自定义的返回值解析器
        myRequestMappingHandlerAdapter.getReturnValueHandlers();
        // myRequestMappingHandlerAdapter.setReturnValueHandlers(returnValueHandlers);
        return myRequestMappingHandlerAdapter;
    }
}
